package com.hzdl.cinema.dao.impl;

import com.hzdl.cinema.dao.RefundDao;
import com.hzdl.cinema.domain.Ticket;
import com.hzdl.cinema.util.JDBCUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class RefundDaoImpl implements RefundDao {
    JdbcTemplate jdbcTemplate =new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public int totalRefund(String select_input) {
        if(select_input!=null){
            if(select_input.equals("")||select_input.equals("null")){
                select_input=null;
            }
        }

        String sql="select count(*) from ticket where ticket_Status=1 and if(? is null,1=1,ticket_Id =? )";
        Integer integer = jdbcTemplate.queryForObject(sql, Integer.class,select_input,select_input);
        return integer;
    }

    @Override
    public List<Ticket> fiedRefund(int index, Integer rows) {

        String sql="select * from  ticket where ticket_Status=1  limit ?,? ";
        List<Ticket> ticketList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Ticket.class),index, rows);

        return ticketList;
    }

    @Override
    public List<Ticket> fiedAllRefund(String select_input) {
        if(select_input!=null){
            if(select_input.equals("")||select_input.equals("null")){
                select_input=null;
            }
        }
        String sql="select * from  ticket where ticket_Status=1 and if (? is null ,1=1,ticket_Id=?)";
        List<Ticket> ticketList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Ticket.class),select_input,select_input);

        return ticketList;
    }

    @Override
    public void updateTicket(String id) {


                String sql = "update ticket set ticket_Status=3 where  ticket_Id = ?";
                jdbcTemplate.update(sql, id);

        }


    @Test
    public  void  mothed(){
        int i = totalRefund("597");
        System.out.println(i);
        List<Ticket> tickets = fiedAllRefund("697");

        System.out.println(tickets);
        String id="198";
        updateTicket(id);

    }
}
